//
//  FLTextView.m
//  BaseRepo
//
//  Created by leo on 2018/8/9.
//  Copyright © 2018年 LEO. All rights reserved.
//

#import "FLTextView.h"
#import "FLLabel.h"
#import "Masonry.h"

@interface FLTextView ()


@property (nonatomic, strong) FLLabel *placeholderLabel;

@end

@implementation FLTextView

#pragma mark- life cycle
+ (FLTextView *(^)(void))fl_init {
    return ^{
        FLTextView *textView = [[FLTextView alloc] init];
        [textView p_initialization];
        return textView;
    };
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




#pragma mark- custom method
- (void)p_initialization {
    [self addSubview:self.placeholderLabel];
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(5);
        make.top.equalTo(self).offset(7);
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textDidEditor:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
    
}

- (void)textDidEditor:(NSNotification *)noti {
    self.placeholderLabel.hidden = self.hasText;
    
}





#pragma mark- setter and getter
- (FLTextView *(^)(CGRect))fl_frame {
    return ^(CGRect frame){
        self.frame = frame;
        return self;
    };
}

- (FLTextView *(^)(UIFont *))fl_font {
    return ^(UIFont *font){
        self.font = font;
        self.placeholderLabel.fl_font(font);
        return self;
    };
}

- (FLTextView *(^)(UIColor *))fl_textColor {
    return ^(UIColor *color){
        self.textColor = color;
        return self;
    };
}

- (FLTextView *(^)(UIColor *))fl_colorForPlaceholder {
    return ^(UIColor *color){
        self.placeholderLabel.fl_textColor(color);
        return self;
    };
}

- (FLTextView *(^)(NSString *))fl_placeholder {
    return ^(NSString *text){
        self.placeholderLabel.fl_text(text);
        return self;
    };
}



- (FLLabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = FLLabel.fl_init();
    }
    return _placeholderLabel;
}
@end
